﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;

public class ControlCenter : MonoBehaviour {

	public float RealGameTime;
	[Space(30)]
	[HideInInspector]
	public float GameTime;
	public float GameTimeLimit;
	public float GameValue
	{
		get
		{
			if (GameTime < GameTimeLimit)
				return  1 - 0.75f * (GameTime / GameTimeLimit);
			else
				return  0.25f;
		}
	}
	void Update()
	{
		RealGameTime += UnityEngine.Time.deltaTime;
		GameTime += UnityEngine.Time.deltaTime/YearRate;
		distances.MinRange = distances.MinRangeSet + GameTime / GameTimeLimit * (transform.Find ("Generator").GetComponent<Generator> ().Rad - distances.MinRangeSet);
	}

	private static volatile ControlCenter instance;   
	private static object syncRoot = new UnityEngine.Object();   
	public static ControlCenter Instance   
	{   
		get    
		{   
			if (instance == null)    
			{   
				lock (syncRoot)    
				{   
					if (instance == null)  {  
						ControlCenter[] instances = FindObjectsOfType<ControlCenter>();  
						if (instances != null){  
							for (var i = 0; i < instances.Length; i++) {  
								Destroy(instances[i].gameObject);  
							}  
						}  
						GameObject go = new GameObject("ControlCenter");  
						instance = go.AddComponent<ControlCenter>();  
						DontDestroyOnLoad(go);   
					}  
				}   
			}   
			return instance;   
		}   
	}  

	[HideInInspector]
	public List<GameObject> Players;
	public GameObject[] PlayersPrefab;
	void Awake()
	{
		instance = this;
		GameObject PlayerCenter = GameObject.Find ("PlayerCenter");
		Players = new List<GameObject> ();
		for (int i = 1; i <= PlayersPrefab.Length; i++) {
			GameObject clone;
			clone =	Instantiate (PlayersPrefab[i-1]);
			clone.transform.parent = PlayerCenter.transform;
			clone.GetComponent<Player> ().PlayerID = i;
			Players.Add (clone);
		}
	}
	public float YearRate;
	public int FindTimes;
	[Serializable]
	public class BaseResourceUse
	{
		public float Explore;
		public float Colony;
		public float Destroy;
	}
	[SerializeField]
	public BaseResourceUse baseResourceUse;
	[Serializable]
	public class SpeedAndTime
	{
		public float LightSpeed;
		public float ExploreCD;
		public float ComubicatedCD;
	}
	[SerializeField]
	public SpeedAndTime speedAndTime;
	[Serializable]
	public class Distances
	{
		public float MinRangeSet;
		[Header("Value")]
		public float MinRange;
	}
	[SerializeField]
	public Distances distances;
	[Serializable]
	public class ActionSpeed
	{
		public float Base;
		public float Explore;
		public float Colony;
		public float Destroy;
	}
	[SerializeField]
	public ActionSpeed actionSpeed;
}
